package game;

import dou.scheduled.TimerManager;
import game.database.mysql.MysqlUtils;
import game.services.ServicesProtocolManager;
import game.socket.ClientSocketManager;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

/**
 * ShutdownHookThread 类在应用关闭时处理一些事务
 * @author wizardc
 */
public class ShutdownHookThread extends Thread
{
    private static Logger logger = LogManager.getLogger("GameServer");

    /**
     * 创建一个 ShutdownHookThread 对象
     */
    public ShutdownHookThread()
    {
    }

    @Override
    public void run()
    {
        logger.info("Game server shutdown begin.");
        try
        {
            //TODO: 停止所有服务
            ServicesProtocolManager.cleanupProtocols();
            TimerManager.getInstance().cleanup();
            MysqlUtils.closeAllConnection();
            ClientSocketManager.getInstance().cleanup();
        }
        catch(Exception exception)
        {
            logger.error("Game server shutdown error: ", exception);
        }
        logger.info("Game server shutdown finished.");
    }
}
